define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {
            // 流程
            $scope.phases = [
                'lot_no',
                'device',
                'panel_no'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                $scope.info_detail = {
                    lot_no:'',
                    item_spec:'',
                    product_no:'',
                    vehicle_no:''
                }
                $scope.panel_detail=[];
                // oliver特製資料
                // $scope.panel_detail=[
                //     {
                //     order:1,
                //     panel_no:'leti 1',
                //     pcs_on_per_panel:5,
                //     origin_pcs_no:5
                //     },
                //     {
                //         order:2,
                //         panel_no:'leti 2',
                //         pcs_on_per_panel:5,
                //         origin_pcs_no:5
                //     }
                // ];

                $scope.phases_index = 0;
                
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            }; 

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if($scope.phases_index == 0)
                    //回 生產批
                    {
                        $scope.info_detail={};
                        $scope.panel_detail=[];
                    } 
                    else if($scope.phases_index == 1)//回 載具
                    {
                        $scope.info_detail[0].vehicle_no='';
                        $scope.panel_detail=[];
                    }
                }
            };


            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // 生產批 流程
                    if($scope.phases_index == 0){
                        lot_no_info_get(barcode,function(data){
                            if(data!=''){
                                $scope.info_detail=data;
                                $scope.phases_index=1;
                            }else{
                                $scope.$root.showAlert($filter('translate')('SMT16.msg.column03'));
                            }
                            
                        });
                    // 載具 流程
                    } else if($scope.phases_index == 1) {
                        vehicle_no_info_get(barcode,function(data){
                            if(data!=''){
                                $scope.info_detail[0].vehicle_no=data[0].vehicle_no;
                                $scope.phases_index=2;  
                            }else{
                                $scope.$root.showAlert($filter('translate')('SMT16.msg.column04'));
                            }
                        });
                    // 板號 流程
                    } else if($scope.phases_index == 2){
                        let panel_no_is_unique=false;
                        if($scope.panel_detail.length!=0){ 
                            $scope.panel_detail.forEach(function(item){
                                if(item.panel_no == barcode){
                                    panel_no_is_unique=true;
                                } 
                            })
                        }
                        if(!panel_no_is_unique){//如果板号是唯一才执行
                            panel_no_info_get(barcode,function(data){
                                if(data!=''){
                                    if(data[0].panel_no==''){
                                        data[0].panel_no == 1;
                                        data[0].panel_no_data = true;
                                    }
                                    data.forEach(function(item){
                                        if(item.pcs_on_per_panel===''||!item.pcs_on_per_panel){item.pcs_on_per_panel='1'}
                                        item.origin_pcs_no = item.pcs_on_per_panel
                                        $scope.panel_detail.unshift(item);
                                    });
                                    $scope.panel_detail.forEach(function(item,index){
                                        item.seq=index+1;
                                    }) 
                                }else{
                                    $scope.$root.showAlert($filter('translate')('SMT16.msg.column05'));
                                }
                            });
                        }else{
                            $scope.$root.showAlert($filter('translate')('SMT16.msg.column06'));
                        }
                    };
                }else{
                    $scope.$root.showAlert($filter('translate')('SMT16.msg.column02'));
                };
            };

            $scope.clean=function(){
                $scope.info_detail=[];
                $scope.panel_detail=[];
                $scope.phases_index = 0;
            }

            // 點擊確認按鈕送回資料
            $scope.confirm = function(){
                var pass = $scope.panel_detail.every(function(item){
                    return item.pcs_on_per_panel <= item.origin_pcs_no;
                })

                if(pass){
                    var content = {
                        lot_no: $scope.info_detail[0].lot_no,
                        product_no: $scope.info_detail[0].product_no,
                        item_spec: $scope.info_detail[0].item_spec,
                        vehicle_no: $scope.info_detail[0].vehicle_no,
                        panel_detail: $scope.panel_detail,
                    };
                    vehicle_save_process(content, function(){
                        $scope.$root.showToast($filter('translate')('SMT16.msg.success'));
                        $scope.info_detail[0].vehicle_no='';
                        $scope.panel_detail=[];
                        $scope.phases_index=1;
                    });
                }else{
                    $scope.$root.showAlert($filter('translate')('SMT16.msg.column01'));
                }
            };

            // 確認 
            function vehicle_save_process(content,callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.vehicle_save_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }

            // 刪除 產品序號
            $scope.removeRow = function (item) {
                $scope.panel_detail.splice($scope.panel_detail.indexOf(item), 1);
            };

            // 取得 生產批資料
            function lot_no_info_get(lot_no,callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'vehicle_lot_info_get',
                    query_condition_info:[
                        {column_name:'a.LOTNO', query_mode:'0', value: lot_no, merge_condition_model:'AND'}
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };
            
            // 取得 載具 資料
            function vehicle_no_info_get(vehicle_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'vehicle_info_get',
                    query_condition_info:[
                        {column_name:'a.ToolNo', query_mode:'0', value: vehicle_no, merge_condition_model:'AND'}
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };
            
            // 取得 板號 資料
            function panel_no_info_get(panel_no, callback){
                var content = {
                    panel_no : panel_no,
                    lot_no:$scope.info_detail[0].lot_no,
                    vehicle_no:$scope.info_detail[0].vehicle_no
                };
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.vehicle_panel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
